package ms.db.mongodb;

import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotEmpty;
import java.util.Map;
import java.util.TreeMap;

@Validated
public class MongoReplicaSetProperties {
    @NotEmpty
    private String replicaSet;
    @NotEmpty
    private String database;
    @NotEmpty
    private String hosts;
    private String username;
    private String password;
    private String authenticationDatabase;
    private Integer minConnectionsPerHost = 10;
    private Integer connectionsPerHost = 2;


    public String getHosts() {
        return hosts;
    }

    public TreeMap<String, Integer> getHostMap() {
        TreeMap<String, Integer> hostMap = new TreeMap<>();
        for (String host : hosts.split(",")) {
            if (host.indexOf(":") == -1) {
                throw new RuntimeException("mongodb host configuration error!");
            }

            String ip = host.split(":")[0];
            Integer port = Integer.parseInt(host.split(":")[1]);
            hostMap.put(ip, port);
        }
        return hostMap;
    }

    public void setHosts(String hosts) {
        this.hosts = hosts;
    }

    public String getReplicaSet() {
        return replicaSet;
    }

    public void setReplicaSet(String replicaSet) {
        this.replicaSet = replicaSet;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAuthenticationDatabase() {
        return authenticationDatabase;
    }

    public void setAuthenticationDatabase(String authenticationDatabase) {
        this.authenticationDatabase = authenticationDatabase;
    }

    public Integer getMinConnectionsPerHost() {
        return minConnectionsPerHost;
    }

    public void setMinConnectionsPerHost(Integer minConnectionsPerHost) {
        this.minConnectionsPerHost = minConnectionsPerHost;
    }

    public Integer getConnectionsPerHost() {
        return connectionsPerHost;
    }

    public void setConnectionsPerHost(Integer connectionsPerHost) {
        this.connectionsPerHost = connectionsPerHost;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }
}
